<template>
  <el-dialog title="新增角色" :visible.sync="dialogVisible" width="30%" :append-to-body="true">
    <el-form ref="addForm" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="菜单权限" prop="auth_codes">
        <TreeSelect
          :data="data"
          :width="432"
          multiple
          nodeKey="key"
          :checkedKeys="defaultCheckedKeys"
          @popoverHide="popoverHide"
        ></TreeSelect>
      </el-form-item>
      <el-form-item label="备注">
        <el-input type="textarea" v-model="form.note"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('addForm')">保存</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import TreeSelect from "@/components/basic-treeselect/treeselect";
import { baseUrl } from "@/config/env";
const urls = {
  checkField: baseUrl + "/zweixin/common/check_unique", // 验证编码唯一性
  save: baseUrl + "/zweixin/sys/role/add",
  list: baseUrl + "/zweixin/sys/auth/list"
};
export default {
  data() {
    return {
      dialogVisible: false,
      data: [],
      defaultCheckedKeys: [],
      form: {
        name: null,
        note: null,
        auth_codes: null
      },
      rules: {
        name: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              axios
                .get(urls.checkField, {
                  params: { table: "t_fk_role", field: "name", value: value }
                })
                .then(res => {
                  if (res.data.result) {
                    callback("角色名称必须唯一");
                  } else {
                    callback();
                  }
                });
            },
            trigger: "blur"
          }
        ],
        auth_codes: [
          { required: true, message: "请选择菜单权限", trigger: "blur" }
        ]
      }
    };
  },
  components: {
    TreeSelect
  },
  methods: {
    popoverHide(checkedIds, checkedData) {
      this.form.auth_codes = checkedIds.toString();
    },
    show() {
      this.dialogVisible = true;
      if (this.$refs["addForm"]) {
        this.$refs["addForm"].resetFields();
      }
      this.fetch();
    },
    onSubmit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          axios.post(urls.save, this.form).then(res => {
            this.$message(res.data.result);
            this.dialogVisible = false;
            this.$emit("refresh");
          });
        }
        return false;
      });
    },
    fetch() {
      axios.get(urls.list).then(res => {
        this.data = res.data.result;
      });
    }
  }
};
</script>